#!/bin/sh
### BEGIN INIT INFO
# Provides:          turnserver
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start turnserver at boot time
# Description:       Enable service provided by turnserver.
### END INIT INFO

set -e

DAEMON=/usr/local/sbin/turnserver 
NAME=turnserver
CONFIG=/usr/local/etc/turnserver.conf

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

case "$1" in
  start)
    echo "Starting $NAME"
    [ -d /var/run/turnserver ] || mkdir -p /var/run/turnserver
    start-stop-daemon --start --background -m --pidfile /var/run/turnserver/turnserver.pid --exec $DAEMON -- -c $CONFIG
		echo "."
    ;;
  stop)
    echo "Stopping $NAME"
    start-stop-daemon --stop --pidfile /var/run/turnserver/turnserver.pid --oknodo --exec $DAEMON --
    rm -f /var/run/turnserver/turnserver.pid
		echo "."
      ;;
  restart)
    echo "Restarting $NAME"
    start-stop-daemon --stop --pidfile /var/run/turnserver/turnserver.pid --oknodo --exec $DAEMON --
    rm -f /var/run/turnserver/turnserver.pid
    sleep 2
    start-stop-daemon --start --background -m --pidfile /var/run/turnserver/turnserver.pid --exec $DAEMON -- -c $CONFIG
		echo "."
    ;;
  status)
        status_of_proc -p "$PIDFILE" "$DAEMON" turnserver && exit 0 || exit $?
   ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

